package com.wdc.patterns.proxy.jdk_proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyFactory {

    //声明目标对象
    private TranStation station = new TranStation();

    public SellTickets getProxyObject() {
        /**
         * ClassLoader loader: 类加载器，用于加载代理类。可以同通过目标对象获取类加载器
         * Class<?>[] interfaces,
         * InvocationHandler h
         */
        SellTickets proxyProject =(SellTickets) Proxy.newProxyInstance(station.getClass().getClassLoader(),
                station.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("收取一定服务费");
                        Object invoke = method.invoke(station, args);
                        return invoke;
                    }
                });
        return proxyProject;
    }


}
